<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *商品管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$manage_op=array('confirm','cancel','agree','disagree');
if(in_array($_RQ['op'],$manage_op)){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择商品！','error');
	}
}
$return=array();
$tabs=array(
	array('op'=>'list','title'=>'商品列表'),
	
);
$return['status']=array('0'=>'下架','1'=>'上架','2'=>'售罄','3'=>'回收站');
//上架商品
if($_RQ['op']=='list'){
	$setting=$this->plugin_setting('setting');
	if($setting['group_id']){
		$params=array('pcate'=>$setting['group_id']);
	}else{
		exi('没有绑定商品！请先绑定！','error','goods_set/');
	}
	
	$params['uniacid']=$_SESSION['uniacid'];
	$params['status']='1';
	if(!empty($_RQ['name'])){
		$params['name like']='%'.trim($_RQ['name']).'%';
		$return['params']['name']=$_RQ['name'];
	}
	if(!empty($_RQ['pcate'])){
		$params['pcate']=intval($_RQ['pcate']);
		$return['params']['pcate']=intval($_RQ['pcate']);
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	if($_RQ['num']){
		$psize=$_RQ['num'];
	}
	$total='';
	$fields=array('id','uniacid','paixu','title','image','pcate','ccate','price','status','createtime','total','salenum','unit','saletype');
	$list=pdo_getslice('jy_weishop_goods',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
				$item['unit']=$item['unit']?'/'.$item['unit']:'/件';
				$item['pcatetitle']=pdo_getcolumn('jy_weishop_goods_group',array('id'=>$item['pcate']),'title');
				$item['pcatetitle']=$item['pcatetitle']?$item['pcatetitle']:'无';
				$item['ccatetitle']=pdo_getcolumn('jy_weishop_goods_group',array('id'=>$item['ccate']),'title');
				$item['ccatetitle']=$item['ccatetitle']?$item['ccatetitle']:'无';
				$item['url']=custom_url('jy_weishop/goods.detail//id='.$item['id'],true);
		}
	}
	
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','edit'=>true,'del_one'=>true,'delall'=>true,'list'=>array(
			array('name'=>'paixu','title'=>'排序','type'=>'string'),
			array('type'=>'img_string','title'=>'商品名称','img'=>'image',
			'data'=>array(
				array('title'=>'','name'=>'title'),
				array('title'=>'价格：￥','name'=>'price')
				)
			),
			array('type'=>'img_string','title'=>'商品分类',
			'data'=>array(
				array('title'=>'一级：','name'=>'pcatetitle'),
				array('title'=>'二级：','name'=>'ccatetitle')
				)
			),
			array('type'=>'img_string','title'=>'库存/销量',
			'data'=>array(
				array('title'=>'库存：','name'=>'total'),
				array('title'=>'销量：','name'=>'salenum','unit'=>'unit')
				)
			),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
			array('name'=>'saletype','title'=>'商品属性','type'=>'checkbox','options'=>'saletypes'),
			array('name'=>'url','title'=>'复制链接','type'=>'copy'),
		)),
		'search_one'=>array('name'=>'title','title'=>'商品名称','add'=>false),
	);
	$return['type']=array('0'=>'实体商品','1'=>'虚拟商品','2'=>'虚拟物品（卡密）','3'=>'批发商品','4'=>'记时/记次商品');
	$return['saletypes']=array('1'=>'推荐','2'=>'新品','3'=>'热卖','4'=>'包邮','5'=>'不参与会员折扣');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_weishop_goods',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		if($item['istime']==1){
			$item['cuxiao']=2;
		}
		elseif($item['isdiscount']==1){
			$item['cuxiao']=1;
		}
		//获取分类
		$pname=pdo_getcolumn('jy_weishop_goods_group',array('id'=>$item['pcate']),'title');
		$item['cid']=array('title'=>$pname,'cid'=>$item['ccate']);
		
		$specs_list=pdo_getall('jy_weishop_goods_spec',array('goodsid'=>$id,'uniacid'=>$_SESSION['uniacid']),array('id','title'),'','paixu asc');
		
		if($specs_list){
			foreach($specs_list as &$row){
				$row['options']=pdo_getall('jy_weishop_goods_spec_item',array('specid'=>$row['id'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'','paixu asc');
			}
			$item['specs']['specs']=$specs_list;
		}
		$option_list=pdo_getall('jy_weishop_goods_option',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$id),array('id','specs','thumb','stock','price','productprice','costprice','goodssn','productsn','weight'),'','paixu asc');
		if($option_list){
			foreach($option_list as &$i){
				$i['specs']=explode('_',$i['specs']);
				foreach($i['specs'] as $j){
					$op=pdo_get('jy_weishop_goods_spec_item',array('id'=>intval($j)),array('specid','title'));
					$spec_title=pdo_getcolumn('jy_weishop_goods_spec',array('id'=>$op['specid']),'title');
					$i[$spec_title]=$op['title'];
				}
				$i['num']=$i['stock'];
				$i['old_price']=$i['productprice'];
				$i['cost_price']=$i['costprice'];
				$i['bm']=$i['goodssn'];
				$i['tm']=$i['productsn'];
				$i['image']=$i['thumb'];
			}
			$item['specs']['tds']=$option_list;
		}
	}else{
		$item=array();
	}
	$fields=pdo_get('jy_weishop_diyform_temp',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$id));
	
	$fields=json_decode($fields['fields'],true);

	$return['item']=$item;
	$return['item']['param']=json_decode($return['item']['param'],true);
	$return['item']['images']=json_decode($return['item']['images'],true);
	$return['item']['istime_time']=json_decode($return['item']['istime_time'],true);
	$return['item']['isdiscount_time']=json_decode($return['item']['isdiscount_time'],true);
	$return['item']['presellonlinetime']=json_decode($return['item']['presellonlinetime'],true);
	$return['item']['presellsendtime']=json_decode($return['item']['presellsendtime'],true);
	$return['item']['fields']=$fields;
	$return['item']['pcd']=json_decode($return['item']['pcd']);
	$return['pcd']=cfc('tools')->pcd();
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回商品列表'),
		),
		'edit_tab'=>true,
		'edits'=>array(
			array(
				'title'=>'基本信息',
				'list'=>array(
					array('name'=>'paixu','title'=>'排序','type'=>'string','help'=>'数字越大，排名越靠前'),
					array('name'=>'title','title'=>'商品名称','type'=>'string'),
					array('name'=>'image','title'=>'商品图片','type'=>'image'),
					array('name'=>'subtitle','title'=>'副标题','type'=>'text','help'=>'副标题的长度请控制在100字以内'),
					array('name'=>'shorttitle','title'=>'短标题','type'=>'string','help'=>'商品短标题 用于快递打印,以及小型热敏打印机打印'),
					array('name'=>'keywords','title'=>'关键字','type'=>'string','help'=>'商品关键字,能准确搜到商品的,比如 : 海尔电视|电视 之类的'),
					array('name'=>'type','title'=>'商品类型','type'=>'radio','options'=>'type'),
					array('name'=>'cid','title'=>'分类','type'=>'goodgroup','options'=>'cid'),
					array('name'=>'ispresell','title'=>'预售设置','type'=>'radio','options'=>'ispresell'),
					array('name'=>'presellprice','title'=>'预售价格','type'=>'string','show'=>array('ispresell'=>1)),
					array('name'=>'presellonlinetime','title'=>'预售时间','type'=>'time_range','show'=>array('ispresell'=>1)),
					array('name'=>'presellsendtimetype','title'=>'预售发货时间类型','type'=>'radio','options'=>'presellsendtimetype','show'=>array('ispresell'=>1)),
					array('name'=>'presellsendtime','title'=>'预售发货时间','type'=>'time','show'=>array('presellsendtimetype'=>0)),
					array('name'=>'presellovertime','title'=>'预售多少天后转为正常商品','type'=>'string','show'=>array('presellsendtimetype'=>1)),
				    array('name'=>'saletype','title'=>'商品属性','type'=>'checkbox','options'=>'saletype'),
					array('name'=>'price','title'=>'商品价格','type'=>'string'),
					array('name'=>'originalprice','title'=>'原价','type'=>'string'),
					array('name'=>'costprice','title'=>'成本价','type'=>'string'),
					array('name'=>'images','title'=>'商品图片','type'=>'images'),
					array('name'=>'salenum','title'=>'已出售数','type'=>'string'),
					array('name'=>'unit','title'=>'商品单位','type'=>'string'),
					array('name'=>'isshowsales','title'=>'是否显示销量','type'=>'switch'),
					array('name'=>'fullnum','title'=>'单品满件包邮','type'=>'string'),
					array('name'=>'fullmoney','title'=>'单品满额包邮','type'=>'string'),
					array('name'=>'fullnoareas','title'=>'不参与单品包邮地区','type'=>'string'),
					array('name'=>'dispatchtype','title'=>'运费设置','type'=>'radio','options'=>'dispatchtype'),
					array('name'=>'dispatchid','title'=>'默认模板','type'=>'select','options'=>'dispatchid','show'=>array('dispatchtype'=>0)),
					array('name'=>'dispatchprice','title'=>'统一运费','type'=>'string','show'=>array('dispatchtype'=>1)),
					array('name'=>'pcd','title'=>'所在地','type'=>'pcd','options'=>'pcd'),
					array('name'=>'iscashier','title'=>'支持收银台','type'=>'switch'),
					array('name'=>'cashpay','title'=>'货到付款','type'=>'switch'),
					array('name'=>'invoice','title'=>'发票','type'=>'switch'),
					array('name'=>'status','title'=>'上架','type'=>'radio','options'=>'status'),
				)
			),
			array(
				'title'=>'库存/规格',
				'list'=>array(
					array('name'=>'goodssn','title'=>'编码','type'=>'string','help'=>''),
					array('name'=>'productsn','title'=>'条码','type'=>'string'),
					array('name'=>'weight','title'=>'重量','type'=>'string','help'=>'单位：克'),
					array('name'=>'isshowtotal','title'=>'展示库存','type'=>'switch'),
					array('name'=>'total','title'=>'库存','type'=>'string'),
					array('name'=>'totalcnf','title'=>'减库存方式','type'=>'radio','options'=>'totalcnf'),
					array('name'=>'specs','title'=>'规格','type'=>'specs'),
				)
			),
			array(
				'title'=>'参数',
				'list'=>array(
						array('type'=>'array','name'=>'param','add_tip'=>'添加一个参数',
									'list'=>array(
										array('type'=>'string','name'=>'title','title'=>'参数名称'),
										array('type'=>'string','name'=>'value','title'=>''),
										
									),
									'details'=>array(
										array('type'=>'string','name'=>'title','title'=>'参数名称'),
										array('type'=>'string','name'=>'value','title'=>'参数值'),							

									)
								),
				)
			),
			array(
				'title'=>'详情',
				'list'=>array(
					array('name'=>'content','title'=>'详情','type'=>'richtext','help'=>''),
					array('name'=>'buyshow','title'=>'购买后可见','type'=>'switch','help'=>'开启后购买过的商品才会显示'),
				)
			),
			array(
				'title'=>'购买权限',
				'list'=>array(
					array('name'=>'maxbuy','title'=>'单次最多购买','type'=>'string','help'=>''),
					array('name'=>'minbuynum','title'=>'单次最低购买','type'=>'string','help'=>''),
					array('name'=>'usermaxbuy','title'=>'最多购买','type'=>'string','help'=>''),
					array('name'=>'showlevels','title'=>'会员等级浏览权限','type'=>'select','options'=>'showlevels','help'=>''),
					array('name'=>'buylevels','title'=>'会员等级购买权限','type'=>'select','options'=>'buylevels','help'=>''),
					array('name'=>'showgroups','title'=>'会员组浏览权限','type'=>'select','options'=>'showgroups','help'=>''),
					array('name'=>'buygroups','title'=>'会员组购买权限','type'=>'select','options'=>'buygroups','help'=>''),
				)
			),
			array(
				'title'=>'营销',
				'list'=>array(
					array('name'=>'isbuyxiu','title'=>'开启买家秀','type'=>'switch','help'=>'开启买家秀后,请移步:营销->基本功能-买家秀！'),
					array('name'=>'cuxiao','title'=>'促销方式','type'=>'radio','options'=>'cuxiao','help'=>''),
					array('name'=>'isdiscount_title','title'=>'促销标题','type'=>'string','show'=>array('cuxiao'=>'1'),'help'=>'例如 : 季末清仓,双十一促销,品牌日 还可以输入 5 个字,不输入默认促销'),
					array('name'=>'isdiscount_time','title'=>'促销时间','type'=>'time_range','show'=>array('cuxiao'=>'1'),'help'=>''),
					array('name'=>'istime_time','title'=>'限时卖时间','type'=>'time_range','show'=>array('cuxiao'=>'2'),'help'=>''),
					array('name'=>'credit','title'=>'赠送积分','type'=>'string','help'=>'会员购物赠送的积分, 如果不填写或填写0，则默认为不赠送积分'),
					array('name'=>'foundmoney','title'=>'余额返现','type'=>'string','help'=>'会员购物返现, 如果不填写或填写0，则默认为不返现'),
					array('name'=>'buyagain','title'=>'重复购买折扣','type'=>'string','help'=>''),
					array('name'=>'buyagain_islong','title'=>'持续使用','type'=>'switch','help'=>'重复购买此商品,享受的折扣'),
					array('name'=>'buyagain_condition','title'=>'使用条件','type'=>'radio','options'=>'buyagain_condition','help'=>'重复购买使用条件,是付款后还是完成后 , 默认是付款后'),
					array('name'=>'buyagain_issale','title'=>'可以使用优惠','type'=>'switch','help'=>'重复购买时,是否与其他优惠共享!其他优惠享受后,在使用这个折扣'),
				)
			),
			array(
				'title'=>'分享关注',
				'list'=>array(
					array('name'=>'needfollow','title'=>'购买强制关注','type'=>'radio','options'=>'needfollow','help'=>''),
					array('name'=>'followtip','title'=>'关注提示','type'=>'string','show'=>array('needfollow'=>'1'),'help'=>''),
					array('name'=>'followurl','title'=>'关注引导链接','type'=>'link','help'=>'购买商品关注，跳转的链接，如果为空默认为系统关注页'),
					array('name'=>'share_title','title'=>'分享标题','type'=>'string','help'=>''),
					array('name'=>'share_icon','title'=>'分享图标','type'=>'image','help'=>''),
					array('name'=>'share_desc','title'=>'分享描述','type'=>'text','help'=>'如果不填写，则使用商品副标题，如商品副标题为空则使用店铺名称'),
				)
			),
			array(
				'title'=>'卖家通知',
				'list'=>array(
					array('name'=>'noticemid','title'=>'选择通知人','type'=>'string','help'=>'填系统会员ID，<a href="./index.php?plugin=core&action=member.member" target="_blank">点击~快速获取会员ID</a>'),
					array('name'=>'noticetype','title'=>'通知方式','type'=>'checkbox','options'=>'noticetype','help'=>''),
					
			)
		),
			array(
				'title'=>'线下核销',
				'list'=>array(
					array('name'=>'isverify','title'=>'支持核销','type'=>'switch','help'=>''),
					array('name'=>'storeids','title'=>'核销门店','type'=>'select','options'=>'storeids','help'=>''),
					array('name'=>'verifytype','title'=>'核销方式','type'=>'radio','options'=>'verifytype','help'=>'按订单核销： 不管购买多少 一次核销完成按次核销： 一个消费码使用多次（购买的数量）按消费码核销： 多个消费码 一次核销一个'),
					array('name'=>'usetime','title'=>'自动使用期限','type'=>'string','help'=>'商品购买X天后自动使用，自动使用后无法退款，设置为0则没有限制'),
			)
		),
			array(
				'title'=>'自定义表单',
				'list'=>array(
					//array('name'=>'diyformtype','title'=>'自定义表单类型','type'=>'radio','options'=>'diyformtype','help'=>''),
							array('type'=>'array','name'=>'fields','add_tip'=>'添加一个字段（温馨提示：已经有用户提交过数据的表单，不允许再编辑和添加新字段！）',
								'list'=>array(
									array('type'=>'string','name'=>'title','title'=>'字段名称'),	
									array('type'=>'radio','name'=>'type','title'=>'字段类型','options'=>'types'),	
								//	array('type'=>'string','name'=>'paixu','title'=>'排序'),	
									array('type'=>'string','name'=>'value','title'=>'选项信息'),	
								),
								'details'=>array(
									array('type'=>'string','name'=>'title','title'=>'字段名称'),	
									array('type'=>'select','name'=>'type','title'=>'字段类型','options'=>'filed_type','option_title'=>'type'),
								//	array('type'=>'string','name'=>'paixu','title'=>'排序','help'=>'从小到大顺序排序'),
									array('type'=>'string','name'=>'value','title'=>'选项信息','help'=>'单选和多选设置此条信息，选项之间以英文分号;隔开，例如:选项1;选项2;选项3'),
								)
								),	
			)
		),
		array(
				'title'=>'店铺信息',
				'list'=>array(
					array('name'=>'store_logo','title'=>'店铺LOGO','type'=>'image','help'=>''),
					array('name'=>'store_title','title'=>'店铺名称','type'=>'string','help'=>''),
					array('name'=>'store_desc','title'=>'店铺描述','type'=>'text','help'=>''),
					array('name'=>'store_btntext1','title'=>'全部商品按钮名称','type'=>'string','help'=>'如果不填写，系统默认"全部商品"'),
					array('name'=>'store_btnurl1','title'=>'全部商品链接','type'=>'link','help'=>'如果不填写，系统默认"默认的全部商品链接"'),
					array('name'=>'store_btntext2','title'=>'进店逛逛按钮名称','type'=>'string','help'=>'如果不填写，系统默认"进店逛逛"'),
					array('name'=>'store_btnurl2','title'=>'进店逛逛链接','type'=>'link','help'=>'如果不填写，系统默认"进店逛逛链接"'),
			)
		),
		)
	);
	//获取分类
	$return['pcate']=pdo_getall('jy_weishop_goods_group',array('uniacid'=>$_SESSION['uniacid'],'parentid'=>0));
	$c=array();
	foreach($return['pcate'] as $v){
		$n=$v['title'];
		$son=pdo_getall('jy_weishop_goods_group',array('uniacid'=>$_SESSION['uniacid'],'parentid'=>$v['id']));
		if($son){
			foreach($son as $s){
				$d=$s['id'];
				$c[$n][$d]=$s['title'];
			}
		}else{
			$c[$n][0]='';
		}
	}
	$return['cid']=$c;
	$type=array('0'=>'实体商品','1'=>'虚拟商品','2'=>'虚拟物品（卡密）','3'=>'批发商品','4'=>'记时/记次商品');
	$saletype=array('1'=>'推荐','2'=>'新品','3'=>'热卖','4'=>'包邮','5'=>'不参与会员折扣');
	$ispresell=array('0'=>'否','1'=>'是');
	$dispatchtype=array('0'=>'默认模板','1'=>'统一运费');
	$status=array('0'=>'下架','1'=>'上架');
	$cuxiao=array('1'=>'促销','2'=>'限时卖');
	$totalcnf=array('0'=>'拍下减库存','1'=>'付款减库存','2'=>'永不减库存');
	$buyagain_condition=array('0'=>'订单付款后','1'=>'订单完成后');
	$needfollow=array('0'=>'否','1'=>'是');
	$noticetype=array('1'=>'付款通知','2'=>'买家收货通知');
	$verifytype=array('0'=>'按订单核销','1'=>'按次核销','2'=>'按消费码核销');
	$diyformtype=array('0'=>'关闭','1'=>'自定义','2'=>'使用模板');
	$presellsendtimetype=array('0'=>'固定时间','1'=>'购买时间');
	$return['storeids']=pdo_getall('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['dispatchid']=pdo_getall('jy_weishop_dispatch',array('uniacid'=>$_SESSION['uniacid'],'status'=>1),array('id','title'),'id');
	$return['type']=$type;
	$return['presellsendtimetype']=$presellsendtimetype;
	$return['cuxiao']=$cuxiao;
	$return['saletype']=$saletype;
	$return['ispresell']=$ispresell;
	$return['dispatchtype']=$dispatchtype;
	$return['totalcnf']=$totalcnf;
	$return['status']=$status;
	$return['buyagain_condition']=$buyagain_condition;
	$return['needfollow']=$needfollow;
	$return['noticetype']=$noticetype;
	$return['verifytype']=$verifytype;
	$return['diyformtype']=$diyformtype;
	$return['htmls']=$htmls;
	$return['filed_type']=array(array('id'=>1,'type'=>'字符串'),array('id'=>2,'type'=>'数字'),array('id'=>3,'type'=>'图片'),array('id'=>4,'type'=>'单选'),array('id'=>5,'type'=>'多选'),array('id'=>6,'type'=>'文本'),array('id'=>7,'type'=>'地址'));
	$return['types']=array('1'=>'字符串','2'=>'数字','3'=>'图片','4'=>'单选框','5'=>'多选框','6'=>'文本','7'=>'地址');
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if($postdata['isbuyxiu']){
		$install = check_permission('jy_vote');
		if(empty($install)){
			exi('没有安装《场景投票》应用，不能开启‘买家秀’！');
		}
	}
	$data=array(
		'title'=>trim($postdata['title']),
		'image'=>trim($postdata['image']),
		'subtitle'=>trim($postdata['subtitle']),
		'shorttitle'=>trim($postdata['shorttitle']),
		'keywords'=>trim($postdata['keywords']),
		'saletype'=>trim($postdata['saletype']),
		'type'=>intval($postdata['type']),
		'paixu'=>intval($postdata['paixu']),
		'ispresell'=>intval($postdata['ispresell']),
		'presellprice'=>intval($postdata['presellprice']),
		'presellsendtime'=>json_encode($postdata['presellsendtime']),
		'presellovertime'=>intval($postdata['presellovertime']),
		'presellsendtimetype'=>intval($postdata['presellsendtimetype']),
		'presellonlinetime'=>json_encode($postdata['presellonlinetime']),
		'price'=>$postdata['price'],
		'originalprice'=>$postdata['originalprice'],
		'marketprice'=>$postdata['marketprice'],
		'costprice'=>$postdata['costprice'],
		'images'=>ijson_encode($postdata['images']),
		'quality'=>intval($postdata['quality']),
		'salenum'=>intval($postdata['salenum']),
		'unit'=>$postdata['unit']?trim($postdata['unit']):'件',
		'isshowsales'=>intval($postdata['isshowsales']),
		'fullnum'=>intval($postdata['fullnum']),
		'fullmoney'=>intval($postdata['fullmoney']),
		'fullnoareas'=>trim($postdata['fullnoareas']),
		'dispatchtype'=>intval($postdata['dispatchtype']),
		'dispatchid'=>intval($postdata['dispatchid']),
		'dispatchprice'=>intval($postdata['dispatchprice']),
		'status'=>intval($postdata['status']),
		'pcd'=>json_encode($postdata['pcd']),
		'iscashier'=>intval($postdata['iscashier']),
		'invoice'=>intval($postdata['invoice']),
		'cashpay'=>intval($postdata['cashpay']),
		
		//库存
		'goodssn'=>trim($postdata['goodssn']),
		'productsn'=>trim($postdata['productsn']),
		'weight'=>intval($postdata['weight']),
		'isshowtotal'=>intval($postdata['isshowtotal']),
		'total'=>intval($postdata['total']),
		'totalcnf'=>intval($postdata['totalcnf']),
		
		//参数
		'param'=>ijson_encode($postdata['param']),
		
		//详情
		'content'=>trim($postdata['content']),
		'buyshow'=>intval($postdata['buyshow']),
		
		//购买权限
		'maxbuy'=>intval($postdata['maxbuy']),
		'minbuynum'=>intval($postdata['minbuynum']),
		'usermaxbuy'=>intval($postdata['usermaxbuy']),
		'showlevels'=>intval($postdata['showlevels']),
		'buylevels'=>intval($postdata['buylevels']),
		'showgroups'=>intval($postdata['showgroups']),
		'buygroups'=>intval($postdata['buygroups']),
		
		//营销
		'isbuyxiu'=>intval($postdata['isbuyxiu']),
		'isdiscount_title'=>trim($postdata['isdiscount_title']),
		'isdiscount_time'=>trim($postdata['isdiscount_time']),
		'istime_time'=>json_encode($postdata['istime_time']),
		'credit'=>intval($postdata['credit']),
		'foundmoney'=>intval($postdata['foundmoney']),
		'buyagain'=>intval($postdata['buyagain']),
		'buyagain_islong'=>intval($postdata['buyagain_islong']),
		'buyagain_issale'=>intval($postdata['buyagain_issale']),
		'buyagain_condition'=>intval($postdata['buyagain_condition']),
		
		//分享关注
		'needfollow'=>intval($postdata['needfollow']),
		'followurl'=>trim($postdata['followurl']),
		'share_title'=>trim($postdata['share_title']),
		'share_icon'=>trim($postdata['share_icon']),
		'share_desc'=>trim($postdata['share_desc']),
		
		//卖家通知
		'noticemid'=>intval($postdata['noticemid']),
		'noticetype'=>trim($postdata['noticetype']),
		
		//核销
		'isverify'=>intval($postdata['isverify']),
		'storeids'=>intval($postdata['storeids']),
		'verifytype'=>intval($postdata['verifytype']),
		'usetime'=>intval($postdata['usetime']),
		
		//店铺信息
		'store_logo'=>trim($postdata['store_logo']),
		'store_title'=>trim($postdata['store_title']),
		'store_desc'=>trim($postdata['store_desc']),
		'store_btntext1'=>trim($postdata['store_btntext1']),
		'store_btnurl1'=>trim($postdata['store_btnurl1']),
		'store_btntext2'=>trim($postdata['store_btntext2']),
		'store_btnurl2'=>trim($postdata['store_btnurl2']),
	);
	if($postdata['cuxiao']){
		if($postdata['cuxiao'] == 1){
			$data['isdiscount']=1;
			$data['istime']=0;
		}
		if($postdata['cuxiao'] == 2){
			$data['isdiscount']=0;
			$data['istime']=1;
		}
	}
	//获取分类
	$ccate = intval($postdata['cid']['cid']);
	if($ccate){
		$pcate = pdo_getcolumn('jy_weishop_goods_group',array('id'=>$ccate),'parentid');
		$data['ccate']=$ccate;
		$data['pcate']=$pcate;
	}else{
		$pcate = pdo_getcolumn('jy_weishop_goods_group',array('title'=>$postdata['cid']['title']),'id');
		$data['pcate']=$pcate;
	}
	
	$goodsid=intval($postdata['id']);
	if($goodsid){
		pdo_update('jy_weishop_goods',$data,array('id'=>$goodsid,'uniacid'=>$_SESSION['uniacid']));
	}else{	
		$data['uniacid']=$_SESSION['uniacid'];
		pdo_insert('jy_weishop_goods',$data);
		$goodsid=pdo_insertid();
	}
	//整理商品规格数据
	if($postdata['specs']){
		$spec_ids=array();//接收当前生效的规格ID
		$spec_option_ids=array();//接收当前生效的规格项ID
		$spec_goods_ids=array();//接收当前生效的规格商品ID
		//第一步，整理规格和规格项
		foreach($postdata['specs']['specs'] as $spec_key=>&$spec){
			if(!$spec['id']){
				
				//数据库里面没有该规格
				$new_spec=array(
					'uniacid'=>$_SESSION['uniacid'],
					'goodsid'=>$goodsid,
					'paixu'=>$spec_key,
					'title'=>$spec['title'],
				);
				pdo_insert('jy_weishop_goods_spec',$new_spec);
				$spec['id']=pdo_insertid();
			}else{
				pdo_update('jy_weishop_goods_spec',array('title'=>$spec['title']),array('id'=>$spec['id']));
			}
			$spec_ids[]=$spec['id'];
			foreach($spec['options'] as $option_key=>&$option){
				if(!$option['id']){
					$new_spec_option=array(
						'uniacid'=>$_SESSION['uniacid'],
						'goodsid'=>$goodsid,
						'specid'=>$spec['id'],
						'title'=>$option['title'],
						'paixu'=>$option_key,
					);
					pdo_insert('jy_weishop_goods_spec_item',$new_spec_option);
					$option['id']=pdo_insertid();
				}else{
					pdo_update('jy_weishop_goods_spec_item',array('title'=>$option['title']),array('id'=>$option['id']));
				}
				$spec_option_ids[]=$option['id'];
			}
		}
		
		
		//第二步，整理规格商品数据
		foreach($postdata['specs']['tds'] as $key=>$spec_goods){
			$data=array(
				'paixu'=>$key,
				'thumb'=>trim($spec_goods['image']),
				'stock'=>intval($spec_goods['num']),
				'price'=>price($spec_goods['price']),
				'productprice'=>price($spec_goods['old_price']),
				'costprice'=>price($spec_goods['cost_price']),
				'goodssn'=>trim($spec_goods['bm']),
				'productsn'=>trim($spec_goods['tm']),
				'weight'=>intval($spec_goods['weight']),
			);
			if($spec_goods['id']){
				pdo_update('jy_weishop_goods_option',$data,array('id'=>intval($spec_goods['id']),'uniacid'=>$_SESSION['uniacid']));
			}else{
				//新的规格商品，根据规格值找到相应规格ID
				$data['specs']=array();
				foreach($postdata['specs']['specs'] as $sp){
					foreach($sp['options'] as $op){
						if($spec_goods[$sp['title']]==$op['title']){
							$data['specs'][]=$op['id'];
							break;
						}
					}
				}
				$data['specs']=implode('_',$data['specs']);
				$data['uniacid']=$_SESSION['uniacid'];
				$data['goodsid']=$goodsid;
				pdo_insert('jy_weishop_goods_option',$data);
				$spec_goods['id']=pdo_insertid();
			}
			$spec_goods_ids[]=$spec_goods['id'];
		}
		//清理数据库中弃用的规格、规格项、规格商品
		if(!empty($spec_ids)){
			pdo_delete('jy_weishop_goods_spec',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$goodsid,'id !='=>$spec_ids));
		}
		if(!empty($spec_option_ids)){
			pdo_delete('jy_weishop_goods_spec_item',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$goodsid,'id !='=>$spec_option_ids));
		}
		if(!empty($spec_goods_ids)){
			pdo_delete('jy_weishop_goods_option',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$goodsid,'id !='=>$spec_goods_ids));
		}
		$options = pdo_getall('jy_weishop_goods_option',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$goodsid),'price','','price asc');
		$options2 = pdo_getall('jy_weishop_goods_option',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$goodsid),'price','','price desc');
		if($options){
			$upgoods =array();
			$upgoods['minprice'] = $options[0]['price'];
			$upgoods['maxprice'] = $options2[0]['price'];
			pdo_update('jy_weishop_goods',$upgoods,array('id'=>$goodsid,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_update('jy_weishop_goods',array('status'=>3),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}